{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using client.Dispatch()\n",
    "\n",
    "Python programs use the win32com.client.Dispatch() method to create COM objects from a ProgID or CLSID. In the example below we use the ProgID, if we wanted to use CLSID we would pass through the GUID of our object in this case {00024500-0000-0000-C000-000000000046}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<win32com.gen_py.Microsoft Excel 16.0 Object Library._Application instance at 0x2388551339424>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<win32com.gen_py.Microsoft Excel 16.0 Object Library._Application instance at 0x2388551339480>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9._Application._Application"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9._Application._Application"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# import our library\n",
    "import win32com.client as win32\n",
    "\n",
    "# Dispatching the object, method one ProgID.\n",
    "ExcelApp1 = win32.Dispatch(\"Excel.Application\")\n",
    "\n",
    "# Dispatching the object, method two CLSID\n",
    "ExcelApp2 = win32.Dispatch(\"{00024500-0000-0000-C000-000000000046}\")\n",
    "\n",
    "display(ExcelApp1)\n",
    "display(ExcelApp2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Early Versus Late Binding.\n",
    "\n",
    "When discussing late versus early binding, really what we are talking about is whether the program language has any advance knowledge of the properties and methods of an object. With late-binding, the program language has no upfront knowledge of the object versus early-binding which means the language does have upfront knowledge of the object. For the win32 library both early and late binding can be implemented.\n",
    "\n",
    "1. win32com.client.Dispatch(\"Excel.Application\") automatically selects the best binding method.\n",
    "2. win32com.client.dynamic.Dispatch(\"Excel.Application\") forces the use of late binding.\n",
    "3. win32com.client.gencache.EnsureDispatch(\"Excel.Application\") forces the use of early binding.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<COMObject Word.Application>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<win32com.gen_py.Microsoft Excel 16.0 Object Library._Application instance at 0x2388551417136>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "win32com.client.dynamic.CDispatch"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9._Application._Application"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import win32com.client \n",
    "from IPython.display import display\n",
    "\n",
    "WordApp = win32com.client.dynamic.Dispatch(\"Word.Application\")\n",
    "ExclApp = win32com.client.gencache.EnsureDispatch(\"Excel.Application\") \n",
    "\n",
    "display(WordApp)\n",
    "display(ExclApp)\n",
    "display(type(WordApp))\n",
    "display(type(ExclApp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<COMObject Excel.Application>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from win32com.client import constants, Dispatch\n",
    "\n",
    "ExclApp = win32com.client.dynamic.Dispatch(\"Excel.Application\") \n",
    "constants.xlPasteAllUsingSourceTheme"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
